//
//  PhotoViewController.m
//  DingDing
//
//  Created by LiynXu on 2016/10/26.
//  Copyright © 2016年 LiynXu. All rights reserved.
//

#import "PhotoViewController.h"


@interface PhotoViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSMutableArray *thumbnailImagesArray;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableDictionary *chooseDict;
@property (nonatomic,strong) UIView *submitView;
@end

@implementation PhotoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"相册";
    self.view.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.submitView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake((ScreenWidth()-100)/2.0, 4, 100, 40);
    button.layer.cornerRadius = 3.0;
    [button setTitle:@"确认选择" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor greenColor];
    [self.submitView addSubview:button];
    
    // Do any additional setup after loading the view.
}

- (void)setPhotoBlock:(void (^)(NSDictionary*))photoBlock{
    _photoBlock = photoBlock;
}

- (void)buttonAction{
    self.photoBlock(self.chooseDict);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setThumbnailImagesArrayWithArray:(NSArray *)thumbnailImagesArray{
    _thumbnailImagesArray = [NSMutableArray arrayWithArray:thumbnailImagesArray];
    [_collectionView reloadData];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PhotoCell alloc] initWithFrame:CGRectMake(0, 0, ItemWidth(), ItemHieght())];
        
    }
    cell.photoImage = self.thumbnailImagesArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    cell.isChoose = [self.chooseDict[string] boolValue];
    NSLog(@"%@",cell.photoImage);
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.thumbnailImagesArray.count;
}
#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ItemWidth(),ItemHieght());
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1, 1, 1, 1);
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell * cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSString *row = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    if (self.chooseDict[row] == nil) {
        if (self.chooseDict.count==5) {
            return;
        }else{
            self.chooseDict[row] = @"YES";
            cell.isChoose = !cell.isChoose;
        }
    }else{
        [self.chooseDict removeObjectForKey:row];
        cell.isChoose = !cell.isChoose;
    }
    NSLog(@"i=%ld r=%ld s=%ld",(long)indexPath.item,indexPath.row,indexPath.section);
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark 懒加载

-(UICollectionView *)collectionView{
    if ( _collectionView == nil) {
        
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0.5;
        flowLayout.minimumInteritemSpacing = 0.5;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 65, ScreenWidth(), ScreenHieght()-108) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        //注册Cell，必须要有
        [_collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
    }
    return  _collectionView;
}

-(NSMutableDictionary *)chooseDict{
    if (_chooseDict == nil) {
        _chooseDict = [NSMutableDictionary dictionary];
    }
    return  _chooseDict;
}

-(UIView *)submitView{
    if (_submitView == nil) {
        _submitView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHieght()-48, ScreenWidth(), 48)];
        _submitView.backgroundColor = [UIColor grayColor];
    }
    return  _submitView;
}
@end
